package com.door.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.door.erp.dwz.DwzResponse;
import com.door.erp.enums.BaseInfoStatus;
import com.door.erp.model.OutputStatus;
import com.door.erp.pager.Pager;
import com.door.erp.sevice.OutputStatusService;

/**
 *类说明
 *
 * @author Xuelin Yang E-mail:yangxuelin@foxmail.com
 * @version 创建时间：2013-6-3 上午9:45:46
 */
@Controller
public class OutputStatusController {
	
	@Resource(name="outputStatusService", type=OutputStatusService.class)
	private OutputStatusService outputStatusService;
	
	@RequestMapping("/outputStatus/outputStatuses.do")
	public ModelAndView outputStatuses(Pager pager) {
		if(pager == null) {
			pager = new Pager();
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("status", BaseInfoStatus.NORMAL);
		int totalCount = outputStatusService.getCount(params);
		pager.setTotalCount(totalCount);
		List<OutputStatus> outputStatuses = outputStatusService.getAll(params, pager);

		ModelAndView mav = new ModelAndView();
		mav.setViewName("outputstatus/outputstatuses");
		mav.addObject("pager", pager);
		mav.addObject("outputStatuses", outputStatuses);
		return mav;
	}
	
	@RequestMapping("/outputStatus/addPage.do")
	public ModelAndView getAddPage() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("outputstatus/add");
		return mav;
	}
	
	@RequestMapping("/outputStatus/add.do")
	public ModelAndView add(OutputStatus outputStatus) {
		outputStatus.setStatus(BaseInfoStatus.NORMAL);
		outputStatusService.add(outputStatus);
		ModelAndView mv = new ModelAndView("dwzresponse");
		DwzResponse resonse = new DwzResponse();
		resonse.setNavTabId("outputstatus");
		resonse.setMessage("添加生产状态成功");
		mv.addObject("response", resonse);
		return mv;
	}
	
	@RequestMapping("/outputStatus/getOutputStatus.do")
	public ModelAndView outputStatuss(@RequestParam(value="id", required=true) Long id) {
		OutputStatus outputStatus = outputStatusService.getObject(id);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("outputstatus/modify");
		mav.addObject("outputStatus", outputStatus);
		return mav;
	}
	
	@RequestMapping("/outputStatus/modify.do")
	public ModelAndView modify(OutputStatus outputStatus) {
		ModelAndView mv = new ModelAndView("dwzresponse");
		DwzResponse resonse = new DwzResponse();
		if(outputStatus.getId() == 1) {
			resonse.setNavTabId("outputstatus");
			resonse.setMessage("此生产状态不能修改");
			mv.addObject("response", resonse);
			return mv;
		}
		outputStatusService.modify(outputStatus);
		resonse.setNavTabId("outputstatus");
		resonse.setMessage("修改生产状态成功");
		mv.addObject("response", resonse);
		return mv;
	}
	
	@RequestMapping("/outputStatus/delete.do")
	public ModelAndView delete(long id) {
		ModelAndView mv = new ModelAndView("dwzresponse");
		DwzResponse resonse = new DwzResponse();
		if(id == 1) {
			resonse.setNavTabId("outputstatus");
			resonse.setMessage("此生产状态不能删除");
			resonse.setCallbackType("");
			mv.addObject("response", resonse);
			return mv;
		}
		OutputStatus outputStatus = outputStatusService.getObject(id);
		outputStatus.setStatus(BaseInfoStatus.DELETED);
		outputStatusService.modify(outputStatus);
		resonse.setNavTabId("outputstatus");
		resonse.setCallbackType("");
		resonse.setMessage("删除生产状态成功");
		mv.addObject("response", resonse);
		return mv;
	}
	
	@RequestMapping("/outputStatus/lookuplist.do")
	public ModelAndView lookuplist(Pager pager) {
		if(pager == null) {
			pager = new Pager();
		}
		ModelAndView mv = new ModelAndView("outputstatus/outputstatus_lookup");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("status", BaseInfoStatus.NORMAL);
		int totalCount = outputStatusService.getCount(params);
		pager.setTotalCount(totalCount);
		List<OutputStatus> outputStatuses = outputStatusService.getAll(params, pager);
		mv.addObject("outputStatuses", outputStatuses);
		return mv;
	}

}
